package com.itheima.app.controller;

import com.itheima.app.manager.MovementManager;
import com.itheima.domain.mongo.Movement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class MovementController {

    @Autowired
    private MovementManager movementManager;

    // 发布动态
    @PostMapping("/movements")
    public void saveMovement(Movement movement, MultipartFile imageContent[]) throws IOException { // json类似不支持文件类型，涉及到文件提交都是普通表单类型
        // 调用manager
        movementManager.saveMovement(movement, imageContent);
    }

    // 查看我的动态、查看指定用户id的动态
    @GetMapping("/movements/all")
    public ResponseEntity findMyMovementByPage(Long userId,
                                               @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 调用manager
        return movementManager.findMyMovementByPage(userId, pageNum, pageSize);
    }

    // 查看好友动态
    @GetMapping("/movements")
    public ResponseEntity findFriendMovementByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 调用manager
        return movementManager.findFriendMovementByPage(pageNum, pageSize);
    }

    // 查看推荐动态
    @GetMapping("/movements/recommend")
    public ResponseEntity findRecommendMovementByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 调用manager
        return movementManager.findRecommendMovementByPage(pageNum, pageSize);
    }

    // 动态点赞
    @GetMapping("/movements/{id}/like")
    public ResponseEntity saveMovementCommentLike(@PathVariable("id") String movementId) {
        // 调用manager
        return movementManager.saveMovementCommentLike(movementId);
    }

    // 动态取消点赞
    @GetMapping("/movements/{id}/dislike")
    public ResponseEntity removeMovementCommentLike(@PathVariable("id") String movementId) {
        // 调用manager
        return movementManager.removeMovementCommentLike(movementId);
    }

    // 动态喜欢
    @GetMapping("/movements/{id}/love")
    public ResponseEntity saveMovementCommentLove(@PathVariable("id") String movementId) {
        // 调用manager
        return movementManager.saveMovementCommentLove(movementId);
    }

    // 动态取消喜欢
    @GetMapping("/movements/{id}/unlove")
    public ResponseEntity removeMovementCommentLove(@PathVariable("id") String movementId) {
        // 调用manager
        return movementManager.removeMovementCommentLove(movementId);
    }

    // 单条动态
    @GetMapping("/movements/{id}")
    public ResponseEntity findMovementVoById(@PathVariable("id") String movementId) {
        // 调用manager
        return movementManager.findMovementVoById(movementId);
    }

    // 评论列表
    @GetMapping("/comments")
    public ResponseEntity findCommentVoByMovementId(
            String movementId,
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 调用manager
        return movementManager.findCommentVoByMovementId(movementId, pageNum, pageSize);
    }

    // 发表评论
    @PostMapping("/comments")
    public void saveMovementComment(@RequestBody Map<String, String> map) {
        // 1.接收参数
        String movementId = map.get("movementId"); // 动态id
        String content = map.get("comment"); // 评论内容
        // 2.调用manager
        movementManager.saveMovementComment(movementId,content);
    }
}
